package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.*;
import com.zzgc.common.entity.*;
import com.zzgc.common.utils.JWTUtil;
import com.zzgc.common.utils.MD5Util;
import com.zzgc.modules.param.AuthUserParam;
import com.zzgc.modules.service.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 用户表(AuthUserParam)表服务实现类
 *
 * @author makejava
 * @since 2022-11-17 15:04:54
 */
@Service("authUserService")
public class AuthUserServiceImpl implements AuthUserService {
    @Resource
    private AuthUserDao authUserDao;
    @Resource
    private AuthUserRoleDao userRoleDao;
    @Resource
    private AuthDepartDao departDao;
    @Resource
    private SchoolDao schoolDao;
    @Resource
    private SchoolTableDao schoolTableDao;
    @Autowired
    private SysDictItemDao sysDictItemDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AuthUser queryById(Long id) {
        return this.authUserDao.queryById(id);
    }

    /**
     * 通过账号和手机号查询用户
     *
     * @param aAccount 账号
     * @param aMobile 手机号
     * @return 实例对象
     */
    @Override
    public List<AuthUser> queryAllByNamePwd(String aAccount, String aMobile) {
        return this.authUserDao.queryAllByNamePwd(aAccount,aMobile);
    }

    /**
     * 分页查询
     *
     * @param authUser 筛选条件
     * @return 查询结果
     */
    @Override
    public Page<AuthUser> queryByPage(AuthUser authUser) {
        long total = this.authUserDao.count(authUser);
        return new PageImpl<>(this.authUserDao.queryAllByLimit(authUser), authUser.getPageable(), total);
    }

    /**
     *  分页查询学校用户
     * @Author
     * @Description
     * @Date  2023/1/16 10:44
     * @Param
     * @return
     */
    @Override
    public Page<AuthUser> queryByPageSchoolName(AuthUser authUser) {
        Long total = this.authUserDao.queryBySchoolNameCount(authUser);
        return new PageImpl<>(this.authUserDao.queryBySchoolNameLimit(authUser), authUser.getPageable(), total);
    }

    /**
     * 新增子用户
     *
     * @param authUser 实例对象
     * @return 实例对象 @Transactional(propagation = Propagation.REQUIRED,readOnly=true)
     */
    @Override
    @Transactional
    public AuthUserParam insertUser(AuthUserParam authUser) {
        this.authUserDao.insert(authUser);
        AuthUserRole authUserRole = new AuthUserRole();
        authUserRole.setAUserId(authUser.getId());
        authUserRole.setARoleId(5L); // 设置学校角色
        authUserRole.setACreateDate(new Date());
        // 添加角色用户关系表
        userRoleDao.insert(authUserRole);
        return authUser;
    }

    /**
     * 新增数据
     *
     * @param authUser 实例对象
     * @return 实例对象 @Transactional(propagation = Propagation.REQUIRED,readOnly=true)
     */
    @Override
    @Transactional // 开启事务
    public AuthUserParam insert(AuthUserParam authUser, HttpServletRequest request) {
        String token = request.getHeader("token");
        String aDepartId = JWTUtil.resolveClaim(token, "aDepartId");
        // 查询添加者的部门信息
        AuthDepart authDepart = departDao.queryById(Long.valueOf(aDepartId != null ? aDepartId : "1"));
        Date date = new Date();
        // 添加部门
        AuthDepart depart = authUser.getDepart();
        SysDictItem sysDictItem = sysDictItemDao.queryByName(depart.getANo());
        String address = depart.getAChainName();
        depart.setANo(sysDictItem.getSName());
        depart.setANote(depart.getAName());
        depart.setAParentIdBak(authDepart.getAParentIdBak());
        depart.setAIdBak(authDepart.getAIdBak());
        depart.setAParentId(authDepart.getId());
        depart.setARegionId(authDepart.getARegionId());
        depart.setALevel(3);
        depart.setAChainName(null);
        depart.setACreateDate(date);
        departDao.insert(depart);
        // 添加学校
        School school = new School();
        school.setSAddress(address);
        school.setSName(depart.getAName());
        school.setSDepartId(authDepart.getId()); // 父级id
        school.setSSchoolDepartId(depart.getId());
        school.setSType(depart.getANo());
        school.setSArea(String.valueOf(authDepart.getARegionId()));
        school.setSCreateDate(date);
        schoolDao.insert(school);
        // 添加学校表
        String schoolTableName = "archives_" + depart.getARegionId() + "_" + school.getId(); // 表名archives_410422_18
        schoolTableDao.addSchoolTable(schoolTableName);
        List<String> list = schoolTableDao.queryTableName(schoolTableName);
        if (list.size() == 0){
            throw new RuntimeException("学校表创建失败!");
        }
        // 密码加密
        authUser.setAPassword("123456");
        authUser.setAPassword(MD5Util.getMD5PWD(authUser.getAPassword()));
        // 通过此路径添加的都是主账号
        authUser.setAState(1);
        // 添加创建时间
        authUser.setACreateDate(date);
        authUser.setABankStatus("0");
        authUser.setADepartId(depart.getId());
        authUser.setASchoolId(school.getId());
        // 添加用户
        int insert = this.authUserDao.insert(authUser);

        AuthUserRole authUserRole = new AuthUserRole();
        authUserRole.setAUserId(authUser.getId());
        authUserRole.setARoleId(5L); // 创建学校角色
        authUserRole.setACreateDate(new Date());
        // 添加角色用户关系表
        userRoleDao.insert(authUserRole);

        if (insert == 0){
            return null;
        }
        return authUser;
    }

    /**
     * 修改数据
     *
     * @param authUser 实例对象
     * @return 实例对象
     */
    @Override
    public AuthUser update(AuthUser authUser) {
        this.authUserDao.update(authUser);
        return this.queryById(authUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.authUserDao.deleteById(id) > 0;
    }
}
